package com.polaris.live.ui.user_space

import android.os.Bundle
import com.bumptech.glide.Glide
import com.gyf.immersionbar.ImmersionBar
import com.polaris.live.R
import com.polaris.live.adapter.GiftDetailAdapter
import com.polaris.live.common.constant.CommonConst
import com.polaris.live.common.mvvm.base2.BaseVbActivity
import com.polaris.live.common.network.parseState
import com.polaris.live.common.util.ToastGravity
import com.polaris.live.constant.UserConst
import com.polaris.live.databinding.ActivityGiftDetailBinding
import com.polaris.live.utils.getLongOrString
import com.polaris.live.utils.image.ImageUtils
import com.polaris.live.utils.route.RoutePath
import com.polaris.live.utils.route.Router
import com.polaris.live.viewmodel.UserSpaceViewModel

/**
 * 礼物详情
 *
 * @author Created by 长空 on 2023/11/01/004 09:42
 */
class GiftDetailActivity :
    BaseVbActivity<UserSpaceViewModel, ActivityGiftDetailBinding>() {

    private val id: Long by lazy { intent.getLongOrString(CommonConst.Gift.GIFT_ID, 0L) }
    private val pic: String by lazy { intent.getStringExtra(CommonConst.Gift.GIFT_PIC).toString() }
    private val name: String by lazy { intent.getStringExtra(CommonConst.Gift.GIFT_NAME).toString() }

    private val mAdapter: GiftDetailAdapter by lazy {
        GiftDetailAdapter()
    }

    override fun initImmersionBar() {
        ImmersionBar.with(this)
            .statusBarDarkFont(false)
            .titleBarMarginTop(mBinding.bvTitle)
            .transparentBar()
            .init()
    }

    override fun initView(savedInstanceState: Bundle?) {
        Glide.with(this).load(ImageUtils.imgPath(pic)).placeholder(R.drawable.img_nothing)
            .error(R.drawable.img_nothing).into(mBinding.ivAvatar)
        mBinding.bvTitle.setCenterTitle(name)
        mBinding.recyclerView.adapter = mAdapter

        mAdapter.setOnItemClickListener{ _, _, position ->
            Router.builder(RoutePath.USER_SPACE_ACTIVITY)
                .withInt("page", UserConst.From.MY)
                .withLong("userId", mAdapter.data[position].userId ?: 0L)
                .navigation(this)
        }

        initObserver()

        mViewModel.getGiftDetail(id)
    }

    private fun initObserver() {
        mViewModel.giftDetailLiveData.observe(this) { resultState ->
            parseState(resultState, {
                mAdapter.setList(it)
            }, {
                ToastGravity.showGravity(it.errorMsg)
            })
        }
    }

}